package com.lcy.struct.bTreePlus.disk.log;

import com.lcy.struct.bTreePlus.disk.TestTree;
import lombok.SneakyThrows;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author lucongyang
 * @date Created in  21:34
 * @description
 */
public class LogUtils {

    private static BufferedWriter bw = null;

    private static final SimpleDateFormat STANDARD_TIME = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

    @SneakyThrows
    public static void log(String msg, Object... o) {
        msg = STANDARD_TIME.format(new Date()) + "\t" + msg;
        msg = msg.replaceAll("\\{}", "%s");
        String logs = String.format(msg, o);
        getBw();
        bw.write(logs);
        bw.write("\n");
    }

    @SneakyThrows
    private static void getBw() {
        if (bw != null) {
            return;
        }
        File file = new File(TestTree.url + ".log");
        FileWriter fileWriter = new FileWriter(file.getAbsoluteFile(), true);
        bw = new BufferedWriter(fileWriter);
    }
}
